package com.dmtt.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dmtt.web.entity.AgvInfo;
import com.dmtt.web.entity.CallPolice;
import com.dmtt.web.service.CarManagerService;
import com.dmtt.web.service.impl.CarAlertServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RabbitMqService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private CarAlertServiceImpl carAlertService;
    @Autowired
    private CarManagerService carManagerService;


    public void makeOrder(){
       try{
            //参数1：交换机。 参数2：路由key/queue队列名称。 参数3：消息内容
            String exchangeName = "direct_order_exchange";
            String routingKey = "";
            List<CallPolice> carAlertVo = carAlertService.callPolice();
            if (carAlertVo.size() != 0) {
                carAlertVo.get(0).setTotal(carAlertVo.size());
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(carAlertVo));
//                System.out.println(jsonArray.toString());
                rabbitTemplate.convertAndSend(exchangeName, "jingbao", jsonArray.toString());
            }

       }catch (Exception e){
           e.printStackTrace();
       }
    }
    public void makeOrder1(){
        try{
            //参数1：交换机。 参数2：路由key/queue队列名称。 参数3：消息内容
            String exchangeName = "direct_order_exchange";
            String routingKey = "";
            List<AgvInfo> agvTarget = carManagerService.findAgvTarget();
            List<AgvInfo> agvInfos = new ArrayList<>();
            for (AgvInfo agv : agvTarget){
                if (agv.getWarningDay()>agv.getWarday()){
                    agvInfos.add(agv);
                }
            }
            if (agvInfos.size() != 0) {
                agvInfos.get(0).setTotal(agvInfos.size());
            }
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(agvInfos));
//            System.out.println(jsonArray.toString());
            rabbitTemplate.convertAndSend(exchangeName,"yujing", jsonArray.toString());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
